package com.android.dopewars;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Fight extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.fight);
        
        color_toggle_ = new Vector<Integer>();
        color_toggle_.add(Color.rgb(64,64,64));
        color_toggle_.add(Color.rgb(84,84,84));
        next_color_ = 0;
        
        // Initialize the fight
        Global.officer_name_ = "Baker Jones";
        Global.officer_health_ = 2 * Global.dealer_.firepower();
        Global.officer_firepower_ = 5;
        Global.officer_num_deputies_ = 1;

    	String attack_message = new String();
    	attack_message += "You are being attacked by ";
    	attack_message += Global.officer_name_;
    	attack_message += " and ";
    	attack_message += Integer.toString(Global.officer_num_deputies_);
    	attack_message += " of his assistants! What do you do?";
    	((TextView)findViewById(R.id.fighting_message)).setText(attack_message);
    	
    	if (Global.dealer_.numGuns() > 0) {
    		((Button)findViewById(R.id.fight_button)).setVisibility(View.VISIBLE);
    	} else {
    		((Button)findViewById(R.id.fight_button)).setVisibility(View.GONE);
    	}
        
        ((Button)findViewById(R.id.fight_button)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		LinearLayout message_container = (LinearLayout)findViewById(R.id.action_messages);
        		message_container.removeAllViews();
        		
        		int damage_dealt = 0;
        		for (int i = 0; i < Global.dealer_.numGuns(); ++i) {
        			damage_dealt += Global.dealer_.gun(i).damage();
        		}
        		TextView next_message = new TextView(v.getContext());
        		next_message.setLayoutParams(new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.FILL_PARENT,
        				LinearLayout.LayoutParams.WRAP_CONTENT));
        		next_message.setTextSize((float)14.0);
        		next_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
        		next_message.setTextColor(Color.WHITE);
        		next_color_ = (next_color_ + 1) % 2;
        		next_message.setText("You blast at " + Global.officer_name_ + "!");
        		message_container.addView(next_message);
        		
        		int deputies_killed = 0;
        		while ((Global.officer_num_deputies_ > 0) && (damage_dealt > 0)) {
        			Global.officer_num_deputies_ -= 1;
        			damage_dealt -= 2;
        			deputies_killed += 1;
        		}
        		if (deputies_killed > 0) {
        			next_message = new TextView(v.getContext());
            		next_message.setLayoutParams(new LinearLayout.LayoutParams(
            				LinearLayout.LayoutParams.FILL_PARENT,
            				LinearLayout.LayoutParams.WRAP_CONTENT));
            		next_message.setTextSize((float)14.0);
            		next_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
            		next_message.setTextColor(Color.WHITE);
            		next_color_ = (next_color_ + 1) % 2;
            		next_message.setText("You killed " + Integer.toString(deputies_killed) + " deputies!");
            		message_container.addView(next_message);
        		}
        		
        		next_message = new TextView(v.getContext());
        		next_message.setLayoutParams(new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.FILL_PARENT,
        				LinearLayout.LayoutParams.WRAP_CONTENT));
        		next_message.setTextSize((float)14.0);
        		next_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
        		next_message.setTextColor(Color.WHITE);
        		next_color_ = (next_color_ + 1) % 2;
        		if (damage_dealt > 0) {
        			Global.officer_health_ -= damage_dealt;
        			next_message.setText("You hit " + Global.officer_name_ + " for " + 
        					Integer.toString(damage_dealt) + " points of damage!");
        		} else {
        			next_message.setText(Global.officer_name_ + " didn't take any damage!");
        		}
        		message_container.addView(next_message);
        		
        		if (Global.officer_health_ <= 0) {
        			int bonus = (int)((((Global.total_days_ - Global.days_left_) / (double)Global.total_days_) * 10.0 + 1.0) * (50000 + Global.rand_gen_.nextDouble() * 100000));
        			Global.dealer_.setCash(Global.dealer_.cash() + bonus);
        			Global.ui_messages_.add("You unfortunately had to kill Baker Jones! You found " + Integer.toString(bonus) + " on him!");
        			Intent i = new Intent();
        			setResult(RESULT_OK, i);
        			finish();
        		} else {
        			int damage = (int)(Global.officer_firepower_ * Global.rand_gen_.nextDouble() + 
        					Global.officer_num_deputies_ * Global.rand_gen_.nextDouble() * 2.0);
        			Global.dealer_.setHealth(Global.dealer_.health() - damage);
        			if (Global.dealer_.health() <= 0) {
        				Global.ui_messages_.clear();
        				Global.ui_messages_.add("You got killed by " + Global.officer_name_ + "!");
        				Intent i = new Intent();
            			setResult(RESULT_OK, i);
            			finish();
        			}
        			TextView damage_message = new TextView(v.getContext());
        			damage_message.setLayoutParams(new LinearLayout.LayoutParams(
            				LinearLayout.LayoutParams.FILL_PARENT,
            				LinearLayout.LayoutParams.WRAP_CONTENT));
        			damage_message.setTextSize((float)14.0);
        			damage_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
        			damage_message.setTextColor(Color.WHITE);
        			damage_message.setText(" You got hit for " + Integer.toString(damage) + 
            				" points of health! You have " + Integer.toString(Global.dealer_.health()) + 
            				" points left!");
            		next_color_ = (next_color_ + 1) % 2;
            		message_container.addView(damage_message);
        		}
        	}
        });
        
        ((Button)findViewById(R.id.run_button)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		double likelihood_of_escape = 1.0;
        		likelihood_of_escape = Math.max(0.05, likelihood_of_escape - Global.officer_firepower_ * 0.1 - 
        				Global.officer_num_deputies_ * 0.05);
        		if (Global.rand_gen_.nextDouble() < likelihood_of_escape) {
        			int lost_money = (int)((0.2 + 0.2 * Global.rand_gen_.nextDouble()) * 
        					Global.dealer_.cash());
        			Global.dealer_.setCash(Global.dealer_.cash() - lost_money);
        			Global.ui_messages_.add("You got away but lost $" + Integer.toString(lost_money) + " while running!");
        			Intent i = new Intent();
        			setResult(RESULT_OK, i);
        			finish();
        		} else {
        			int damage = (int)(Global.officer_firepower_ * Global.rand_gen_.nextDouble() + 
        					Global.officer_num_deputies_ * Global.rand_gen_.nextDouble() * 2.0);
        			Global.dealer_.setHealth(Global.dealer_.health() - damage);
        			if (Global.dealer_.health() <= 0) {
        				Global.ui_messages_.clear();
        				Global.ui_messages_.add("You got killed by " + Global.officer_name_ + "!");
        				Intent i = new Intent();
            			setResult(RESULT_OK, i);
            			finish();
        			}
        			TextView next_message = new TextView(v.getContext());
            		next_message.setLayoutParams(new LinearLayout.LayoutParams(
            				LinearLayout.LayoutParams.FILL_PARENT,
            				LinearLayout.LayoutParams.WRAP_CONTENT));
            		next_message.setTextSize((float)14.0);
            		next_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
            		next_message.setTextColor(Color.WHITE);
            		next_color_ = (next_color_ + 1) % 2;
            		next_message.setText("You couldn't get away! You got hit for " + 
            				Integer.toString(damage) + " points of health! You have " + 
            				Integer.toString(Global.dealer_.health()) + " points left!");
            		LinearLayout message_container = (LinearLayout)findViewById(R.id.action_messages);
            		message_container.removeAllViews();
            		message_container.addView(next_message);
        		}
        	}
        });
        
        ((Button)findViewById(R.id.odd_button)).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		int damage = (int)(Global.officer_firepower_ * Global.rand_gen_.nextDouble() + 
        				Global.officer_num_deputies_ * Global.rand_gen_.nextDouble() * 2.0);
    			Global.dealer_.setHealth(Global.dealer_.health() - damage);
    			if (Global.dealer_.health() <= 0) {
    				Global.ui_messages_.clear();
    				Global.ui_messages_.add("You got killed by " + Global.officer_name_ + "!");
    				Global.days_left_ = 0;
    				Global.current_drugs_.clear();
    				Intent i = new Intent();
        			setResult(RESULT_OK, i);
        			finish();
    			}
    			TextView next_message = new TextView(v.getContext());
        		next_message.setLayoutParams(new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.FILL_PARENT,
        				LinearLayout.LayoutParams.WRAP_CONTENT));
        		next_message.setTextSize((float)14.0);
        		next_message.setBackgroundColor(color_toggle_.elementAt(next_color_));
        		next_message.setTextColor(Color.WHITE);
        		next_color_ = (next_color_ + 1) % 2;
        		next_message.setText("You stand there like a moron and just take it. You get hit for " + 
        				Integer.toString(damage) + " points of health! You have " + 
        				Integer.toString(Global.dealer_.health()) + " points left!");
        		LinearLayout message_container = (LinearLayout)findViewById(R.id.action_messages);
        		message_container.removeAllViews();
        		message_container.addView(next_message);
        	}
        });
    }

    Vector<Integer> color_toggle_ = new Vector<Integer>();
    int next_color_ = 0;
    
}
